home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MPW C++ / MPW C++ 3.1 / Interfaces / CIncludes / OldStream.h < prev    next >
Text File  |  1990-09-11  |  9KB  |  336 lines

  1. /*ident    "@(#)ctrans:incl/Ostream.h    1.1.5.1" */
  2. /**************************************************************************
  3.             Copyright (c) 1984 AT&T
  4.                 All Rights Reserved      
  5.  
  6.     THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
  7.     
  8.     The copyright notice above does not evidence any       
  9.     actual or intended publication of such source code.
  10.  
  11. *****************************************************************************/
  12. /*
  13.     stream.h, the header file for the C++ stream i/o system 
  14. */
  15.  
  16. #ifndef __IOSTREAM__
  17. #define __IOSTREAM__
  18.  
  19. #ifndef __STDIO__
  20. #include <stdio.h>
  21. #endif
  22. //#include <osfcn.h>    // for Macintosh
  23. #ifndef __FCNTL__
  24. #include <FCntl.h>
  25. #endif
  26.  
  27. #define    BUFSIZE    BUFSIZ
  28.  
  29. #ifndef NULL
  30. #define NULL    0
  31. #endif
  32.  
  33. #ifndef EOF
  34. #define    EOF    (-1)
  35. #endif
  36.  
  37. #ifndef BUFSIZE
  38. #ifdef uts
  39. #define BUFSIZE 4096
  40. #else
  41. #define BUFSIZE 1024
  42. #endif
  43. #endif
  44.  
  45. enum state_value { _good=0, _eof=1, _fail=2, _bad=4 };
  46. enum open_mode { input=0, output=1, append=2 };
  47.  
  48. struct streambuf {            // a buffer for streams
  49.  
  50.     char*    base;            // pointer to beginning of buffer
  51.     char*    pptr;            // pointer to next free byte
  52.     char*    gptr;            // pointer to next filled byte
  53.     char*    eptr;            // pointer to first byte following buffer
  54.     char    alloc;            // true if buffer is allocated using "new"
  55.     FILE*    fp;            // for stdio compatibility
  56.     
  57.     virtual overflow(int c=EOF);    // Empty a buffer.
  58.                     // Return EOF on error
  59.                      //        0 on success
  60.  
  61.     virtual int underflow();    // Fill a buffer
  62.                     // Return EOF on error or end of input
  63.                      //      next character on success
  64.     
  65.     inline int sgetc()        // get the current character
  66.     {
  67.         return (gptr>=pptr) ? underflow() : *gptr&0377;
  68.     }
  69.  
  70.     
  71.     inline int snextc()        // get the next character
  72.     {
  73.         return (gptr>=(pptr-1)) ? underflow() : *++gptr&0377;
  74.     }
  75.  
  76.     inline void stossc()        // advance to the next character
  77.     {
  78.         if (gptr++ >= pptr)
  79.             underflow();
  80.     }
  81.  
  82.     inline void sputbackc(char c)
  83.     /*
  84.         Return a character to the buffer (ala lookahead 1).  Since
  85.         the user may be "playing games" the character might be 
  86.         different than the last one returned by sgetc or snextc.
  87.         If the user attempts to put back more characters than have
  88.         been extracted, nothing will be put back.
  89.         Putting back an EOF is DANGEROUS.
  90.     */
  91.     {
  92.         if (gptr > base) *--gptr = c;
  93.     }
  94.  
  95.     inline int sputc(int c =EOF)    // put a character into the buffer
  96.     {
  97.         if (fp == 0)
  98.             return (eptr<=pptr) ? overflow(c&0377) : (*pptr++=c&0377);
  99.         else
  100.             return putc(c, fp);
  101.     }
  102.  
  103.     
  104.     inline streambuf * setbuf(char *p, int len, int count =0)
  105.     /*
  106.         supply an area for a buffer.
  107.         The "count" parameter allows the buffer to start in non-empty.
  108.     */
  109.     {
  110.         base = gptr = p;
  111.         pptr = p + count;
  112.         eptr = base + len;
  113.         return this;
  114.     }
  115.  
  116.     int doallocate();        // allocate some space for the buffer
  117.     inline int allocate(){ return base==0 ? doallocate() : 0; }
  118.  
  119.     streambuf() { base = gptr = pptr = eptr = 0; alloc = 0; fp = 0; }
  120.     streambuf(char* p, int l) { setbuf(p,l); alloc = 0; }
  121.     ~streambuf() { if (base && alloc) delete base; }
  122. };
  123.  
  124. struct filebuf : public streambuf {    // a stream buffer for files
  125.  
  126.     int    fd;            // file descriptor
  127.     char    opened;            // non-zero if file has been opened
  128.  
  129.     int overflow(int c=EOF);    // Empty a buffer.
  130.                     // Return EOF on error
  131.                     //      0 on success
  132.     
  133.     int underflow();        // Fill a buffer.
  134.                      // Return EOF on error or end of input
  135.                      //        next character on success
  136.      
  137.     filebuf* open(/*const*/ char *name, open_mode om);    // Open a file
  138.                              // return 0 if failure
  139.                             // return "this" if success
  140.     int     close() { int i = opened?::close(fd):0; opened=0; return i; }
  141.  
  142.     filebuf() { opened = 0; fp = 0; }
  143.     filebuf(FILE* p) { fp = p; fd = p->_file; opened = 1; }
  144.     filebuf(int nfd) { fd = nfd; opened = 1; }
  145.     filebuf(int nfd, char* p, int l) : (p,l) { fd = nfd; opened = 1; }
  146.     ~filebuf() { close(); }
  147. };
  148.  
  149. struct circbuf : public streambuf {    // a circular stream buffer
  150.  
  151.     int overflow(int c=EOF);    // Empty a buffer.
  152.                      // Return EOF on error
  153.                      //      0 on success
  154.     
  155.     int underflow();        // Fill a buffer.
  156.                     // Return EOF on error or end of input
  157.                      //        next character on success
  158.     circbuf() { }
  159.     ~circbuf() { }
  160.  
  161. };
  162.  
  163. /*
  164.  *    This type defines white space.  Any number of whitespace
  165.  *    characters can be used to separate two fields in an input
  166.  *    stream.  The effect of sending an input stream to a whitespace
  167.  *    object is to cause all whitespace in the input stream, up to the
  168.  *    next non-whitespace character, to be discarded.  The whitespace
  169.  *    characters are space, tab, form feed, and new line.
  170.  */
  171. struct whitespace { };
  172.  
  173. /***************************** output: *********************************/
  174.  
  175. extern char* oct(long, int =0);
  176. extern char* dec(long, int =0);
  177. extern char* hex(long, int =0);
  178.  
  179. extern char* chr(int, int =0);        // chr(0) is the empty string ""
  180. extern char* str(const char*, int =0);
  181. extern char* form(const char* ...);        // printf format
  182.  
  183. class istream;
  184. class common;
  185.  
  186. class ostream {
  187. friend istream;
  188.  
  189.     streambuf* bp;
  190.     short    state;
  191. public:
  192.     ostream& operator<<(const char*);    // write
  193.     ostream& operator<<(int a) { return *this<<long(a); }
  194.     //ostream& operator<<(unsigned a) { return *this<<form("%u",a); }
  195.     ostream& operator<<(unsigned a) 
  196.         { return *this<<(unsigned long) a; }
  197.     ostream& operator<<(unsigned long);
  198.     ostream& operator<<(long);    // beware: << 'a' writes 97
  199.     ostream& operator<<(double d) { return (*this)<<(extended)d; }    // for Macintosh
  200.     ostream& operator<<(extended);                                    // for Macintosh
  201.     // Change for const member function feature in beta6:
  202.     // ostream& operator<<(const streambuf&);
  203.     ostream& operator<<(streambuf&);
  204.     ostream& operator<<(const whitespace&);
  205.     ostream& operator<<(const common&);
  206.  
  207.     ostream& put(char);        // put('a') writes a
  208.     ostream& flush() { bp->overflow(); return *this; }
  209.  
  210.  
  211.         operator void*(){ return _eof<state?0:this; }
  212.     int    operator!()    { return _eof<state; }
  213.     int    eof()        { return state&_eof; }
  214.     int    fail()        { return _eof<state; }
  215.     int    bad()        { return _fail<state; }
  216.     int    good()        { return state==_good; }
  217.     void    clear(short state_val = _good)    { state=state_val; }
  218.     short    rdstate()    { return state; }
  219.     char*    bufptr()    { return bp->base; }
  220.  
  221.         ostream(streambuf* s) { bp = s; state = 0; }
  222.         ostream(int fd) { bp = new filebuf(fd); state = 0; }
  223.         ostream(int size, char* p)
  224.         {
  225.             state = 0;
  226.             bp = new streambuf();
  227.             if (p == 0) p = new char[size];
  228.             bp->setbuf(p, size);
  229.         }
  230.         ~ostream() { flush(); }
  231. };
  232.  
  233. /***************************** input: ***********************************/
  234.  
  235. /*
  236.     The >> operator reads after skipping initial whitespace
  237.     get() reads but does not skip whitespace
  238.  
  239.     if >> fails    (1) the state of the stream turns non-zero
  240.             (2) the value of the argument does not change
  241.             (3) non-whitespace characters are put back onto the stream
  242.  
  243.     >> get() fails if the state of the stream is non-zero
  244. */
  245.  
  246. class istream {
  247. friend ostream;
  248.  
  249.     streambuf*    bp;
  250.     ostream*    tied_to;
  251.     char        skipws;        // if non-null, automaticly skip whitespace
  252.     short        state;
  253.  
  254.     friend void eatwhite (istream&);
  255. public:
  256.     int     skip(int i) { int ii=skipws; skipws=i; return ii; }
  257.  
  258.     /*
  259.         formatted input: >> skip whitespace
  260.     */
  261.     istream& operator>>(char*);            // string
  262.     istream& operator>>(char&);            // single character
  263.     istream& operator>>(short&);
  264.     istream& operator>>(int&);
  265.     istream& operator>>(long&);
  266.     istream& operator>>(float&);
  267.     istream& operator>>(double&);
  268.     istream& operator>>(extended&);        // for Macintosh, add extended support
  269.     istream& operator>>(comp&);            // for Macintosh, add comp support
  270.     istream& operator>>(streambuf&);
  271.     istream& operator>>(whitespace&);        // skip whitespace
  272.     istream& operator>>(common&);
  273.  
  274.     /*
  275.         raw input: get's do not skip whitespace
  276.     */
  277.     istream& get(char*, int, char ='\n');        // string
  278.     istream& get(streambuf& sb, char ='\n');
  279.     istream& get(char& c)                // single character
  280.     {
  281.         int os = skipws;
  282.         skipws = 0;
  283.         *this >> c;
  284.         skipws = os;
  285.         return *this;
  286.     }
  287.  
  288.     istream& putback(char c);
  289.     ostream* tie(ostream* s) { ostream* t = tied_to; tied_to = s; return t; }
  290.  
  291.         operator void*(){ return _eof<state?0:this; }
  292.     int    operator!()    { return _eof<state; }
  293.     int    eof()        { return state&_eof; }
  294.     int    fail()        { return _eof<state; }
  295.     int    bad()        { return _fail<state; }
  296.     int    good()        { return state==_good; }
  297.     void    clear(short state_val = _good)    { state=state_val; }
  298.     short    rdstate()    { return state; }
  299.     char*    bufptr()    { return bp->base; }    
  300.  
  301.         istream(streambuf* s, int sk =1, ostream* t =0)    // bind to buffer
  302.         {
  303.             state = 0;
  304.             skipws = sk;
  305.             tied_to = t;
  306.             bp = s;
  307.         }
  308.  
  309.         istream(int size, char* p, int sk =1)        // bind to string
  310.         {
  311.             state = 0;
  312.             skipws = sk;
  313.             tied_to = 0;
  314.             bp = new streambuf();
  315.             if (p == 0) p = new char[size];
  316.             bp->setbuf(p, size, size);
  317.         }
  318.  
  319.         istream(int fd, int sk =1, ostream* t =0)    // bind to file
  320.         {
  321.             state = 0;
  322.             skipws = sk;
  323.             tied_to = t;
  324.             bp = new filebuf(fd);
  325.         }
  326. };
  327.  
  328.  
  329. extern istream cin;    // standard input predefined
  330. extern ostream cout;    // standard output
  331. extern ostream cerr;    // error output
  332.  
  333. extern whitespace WS;    // predefined white space
  334.  
  335. #endif
  336.